from django.db import models

# Asset, what strategies are about
class Asset(models.Model):
	name = models.CharField(max_length=20, unique=True)

	def __unicode__(self):
		return self.name

# Strategy : repartition of the asset (percentage of the number of actions)
class Strategy(models.Model):
	name = models.CharField(max_length=20, unique=True)
	assetPercentagesInStrat = models.ManyToManyField(Asset, through='AssetPercentageInStrategy')

	def __unicode__(self):
		return self.name

#  Player Object. The player has
# - a main strategy
# - a portfolio value
# - a MtM repartition of his portfolio that is how his MtM is dispatched in different stretgy.
# In a first version, the, this is only one element with 100% on the main strategy
# The number of assets in each strategy is computed as
#
#               (percentage of MtM in the strategy) * (MtM of the portfolio) / (MtM of the strategy)
#
# When another player modify the market the number of assets in each strategy does not move and the MtM of the player changes accordingly
# To modify his main strategy, the player have first to put 100% on his main strategy, change his strategy and go back
# to his initial repartition of MtM
# In the first version, 100% is all ready on the main strategy and nothing has to be done
# In this setup the concept of "Fund" (which is the set of a strategy and the total number of asset associated to it) 
# has no direct implementation in the models
class Player(models.Model):
	name = models.CharField(max_length=20, unique=True)

	# mtm repartition among the strategies. This is fixed when set and each time the player change it's main strategy.
	# when the market evolve this repartition does not hold anymore.
	# each time the player modify it strayegie or its MtM repartition in different strategies,
	# this allow to compute the number of asset per strategy wich remains constant as the market evolve
	# for the folowing member : in a first version the type could be models.ForeignKey(MtMPercentatgeOnStrat), with 100% as a pourcentage
	numberOfAssetsAndMtMPercentagePerStrategy = models.ManyToManyField(Strategy, through='NumberOfAssetsAndMtMPercentageInStrategy')
	mainStrategy = models.CharField(max_length=20, unique=True)
        
	def __unicode__(self):
		return self.name

# PercentatgeInStrat is a relationship between a Strategy and all his assets (formerly Proability)
# the sum of all percentage in AssetPercentatgeInStrat for a strategy muste sum to 100
class AssetPercentageInStrategy(models.Model):
	asset = models.ForeignKey(Asset)
	strategy = models.ForeignKey(Strategy)
	# the percentage is user-defined and is an float
	assetPercentageInStrategy = models.FloatField()

# PercentatgeOnStrat is a relationship between a player portfolio and all the strategies it is invested in.
# In a first version, all the MtMPercentatgeOnStrat should have a percentage = 100% and a player must have only one MtMPercentatgeOnStrat
# In a second version, all the percentage in MtMPercentatgeOnStrat for a player must sum to 100%
class NumberOfAssetsAndMtMPercentageInStrategy(models.Model):
	strategy = models.ForeignKey(Strategy)
	player = models.ForeignKey(Player)
	numberOfAssetsInStrategy = models.FloatField()
	mtMPercentageInStrategy = models.FloatField(default=100)

	
# 3 charts can be interseting:
# - asset prices funtion of time (nAsset curves)
# - player main strategy function of time (nPlayer curves)
# - player portfolio MtM function of time (nPlayer curves)
# this should be used as in: http://stackoverflow.com/questions/4487975/historicity-in-django
class History(models.Model):
	time = models.DateTimeField(auto_now_add=True)
	assets =  models.ManyToManyField(Asset,through='AssetForHistory')
	strategies = models.ManyToManyField(Strategy, through='StrategyForHistory')
	playerPortfolios = models.ManyToManyField(Player, through='PlayerPortfolioForHistory')


# PercentatgeInStrat is a relationship between a Strategy and all his assets (formerly Proability)
# the sum of all percentage in AssetPercentatgeInStrat for a strategy muste sum to 100
class AssetForHistory(models.Model):
	asset = models.ForeignKey(Asset)
	history = models.ForeignKey(History)
	# the percentage is user-defined and is an float
	price = models.FloatField()

# PlayerStrategyMtMForHistory. This class is only use for history purposes
class StrategyForHistory(models.Model):
	strategy = models.ForeignKey(Strategy)
	history = models.ForeignKey(History)
	strategyMtM = models.FloatField()
	strategyPerfSinceLastHistoryDate = models.FloatField()
	numberOfPlayersInvestedInTheStrategy = models.IntegerField()
	totalNumberOfAssetInvestedInTheStrategy = models.FloatField()

# PlayerPortfolioMtMForHistory. This class is only use for history purposes
class PlayerPortfolioForHistory(models.Model):
	player = models.ForeignKey(Player)
	history = models.ForeignKey(History)
	portfolioMtM = models.FloatField()
	investementInMainStrategyPercentage = models.FloatField()
	numberOfStrategies = models.IntegerField()

